package com.amox.uitest.kit;

import java.util.ArrayList;
import java.util.List;

import com.amox.uitest.tcase.BaseTestCase;

import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;

public class OgnlKit {
	public static OgnlContext context = BaseTestCase.context;

	public static OgnlContext context() {
		return OgnlKit.context;
	}
	
	public static void clear() {
		OgnlKit.context.clear();
	}
	
	public static Object put(Object key, Object obj) {
		return OgnlKit.context.put(key, obj);
	}
	
	public static Object get(Object key) {
		try {
			return OgnlKit.context.get(key);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static Object getValue(Object key) {
		try {
			return Ognl.getValue(key, OgnlKit.context, OgnlKit.context.getRoot());
		} catch (OgnlException e) {
//			e.printStackTrace();
			return null;
		}
	}
	
	public static void main(String[] args) throws OgnlException
    {
        Person person = new Person();
        person.setUsername("yujun");

        Dog dog = new Dog();
        dog.setUsername("laifu");
        /**
         * 生成ognl上下文对象
         */
        OgnlContext context = new OgnlContext();

        /**
         * put方法是将对象放置到上下文，以便ognl进行读取解析
         * 
         */
        context.put("person", person);

        context.put("dog", dog);
        
        context.put("xxoo", "xxoo");
        
        /**
         * 设置根元素对象，假如直接读取属性，那么将从根元素寻找此属性值，比如person对象的username属性，
         * 当我们将person设置为根元素后，那么parseExpression方法将从person里面寻找username
         */
        context.setRoot(person);

        Object xxoo = Ognl.getValue("#xxoo + '小明'", context, context.getRoot());
        
        System.out.println("#xxoo = " + xxoo);
        
        Object obj1 = Ognl.parseExpression("username");

        System.out.println(obj1);

        Object obj2 = Ognl.getValue("username", context, context.getRoot());

        System.out.println("username = " + obj2);

        Object obj3 = Ognl.parseExpression("#person.username");

        System.out.println("obj3 " + obj3);

        Object obj4 = Ognl.getValue(obj3, context, context.getRoot());

        System.out.println(obj4);

        Object obj5 = Ognl.parseExpression("#dog.username");

        System.out.println(obj5);

        Object obj6 = Ognl.getValue(obj5, context, context.getRoot());

        System.out.println(obj6);

        /**
         * 获取username属性值的时候，相当于是隐式调用了对应的getUsername()方法
         * 可以再去调用这个获得的字符串的相应的方法，比如说toUpperCase，还可以再调用length
         * 
         */
        Object obj7 = Ognl.parseExpression("username.toUpperCase().length()");

        System.out.println(obj7);

        Object obj8 = Ognl.getValue(obj7, context, context.getRoot());

        System.out.println("obj8 = " + obj8);

        /**
         * 调用静态方法不需要生成对象，格式为\@package.className@methodName(parameters)\
         * 当调用的类是java
         * .lang.Math时，可以省略package.className，形式为@@methodName(parameter)
         */
        Object obj9 = Ognl
                .parseExpression("@java.lang.Integer@toBinaryString(10)");

        System.out.println(obj9);

        Object obj10 = Ognl.getValue(obj9, context, context.getRoot());

        System.out.println(obj10);

        /**
         * 生成一个集合对象的使用方法，用花括号，里面用逗号，要访问某个值，只需要用下标访问即可 对于Ognl来说，集合与数组是一样的
         * 
         */
        Object obj11 = Ognl.getValue("{'aa','ab','ac'}[2]", context,
                context.getRoot());

        System.out.println(obj11);

        dog.setFriends(new String[]
        { "aa", "bb", "cc" });

        Object obj12 = Ognl.getValue("#dog.friends[1]", context,
                context.getRoot());

        System.out.println(obj12);

        List<String> list = new ArrayList<String>();

        list.add("aaaaa");
        list.add("bbb");
        list.add("ccc");

        context.put("list", list);

        Object obj13 = Ognl.getValue("#list[1]", context, context.getRoot());

        System.out.println(obj13);

        /**
         * Ognl处理映射（map）的语法格式 #{'key1':'value1','key2':'value2'};
         * 
         * 如果要获得某个value，传递对应的key即可
         */

        Object obj14 = Ognl.getValue("#{'key1':'value1','key2':'value2'}['key2']", context,
                context.getRoot());

        System.out.println(obj14);

        /**
         * 过滤(filter)的表达式
         * 
         * collection.{? expression}
         * 
         */
        List<Person> persons = new ArrayList<Person>();
        Person p1 = new Person();
        Person p2 = new Person();
        Person p3 = new Person();
        p1.setUsername("zhangsikkkk");
        p2.setUsername("liwu");
        p3.setUsername("zhaoliu");

        persons.add(p1);
        persons.add(p2);
        persons.add(p3);
        context.put("persons", persons);
        /**
         * 过滤操作，返回name值大于4的 #this代表persons对象，操作结果返回的是一个集合collection
         * "#persons.{? #this.username.length() > 4}.isEmpty()"
         * 返回的集合就可以调用集合的方法了，比如size（），比如isEmpty（）
         * 
         * 获取集合中的第一个元素 #persons.{? #this.username.length() >
         * 4}[0].username，可以返回第一个元素的username属性 获取集合中的第一个元素(返回的是一个集合) #persons.{^
         * #this.username.length() > 4}，此表达式返回[perosn@hashcode]
         * 
         * 返回符合条件的最后一个元素(返回的是一个元素) #persons.{$ #this.username.length() >
         * 4}，此表达式返回perosn@hashcode
         * 
         * 
         */
        Object obj15 = Ognl.getValue("#persons.{? #this.username.length() > 4}.size()", context,
                context.getRoot());

        System.out.println(obj15);

        /**
         * 投影(projection)表达式 collection.{expression} 过滤是取行，投影是取列
         * #persons.{username} 返回三个名字的集合
         * 下面这个表达式将名字长度小于5的替换成hello world
         * #persons.{#this.username.length() <= 5 ? 'hello world' : #this.name}
         */
        Object obj16 = Ognl.getValue("#persons.{username}.size()", context,
                context.getRoot());

        System.out.println(obj16);

        Object obj17 = Ognl.getValue("#persons.{#this.username.length() <= 5 ? 'hello world' : #this.username}",
                        context, context.getRoot());

        System.out.println(obj17);

    }
}

class Person {
	private String username;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
}

class Dog {
	private String username;
	
	private String[] friends;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String[] getFriends() {
		return friends;
	}

	public void setFriends(String[] friends) {
		this.friends = friends;
	}
}
